﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using control.Biz;

namespace TrangSucShop.usercontrol
{
    public partial class Details : System.Web.UI.UserControl
    {

        
        protected void Page_Load(object sender, EventArgs e)
        {
            DataList1.ItemCommand += new DataListCommandEventHandler(DataList1_ItemCommand);
            if (!IsPostBack)
            {
                if (Request.QueryString["IDSP"] != null)
                {
                    List<BizSanPham> sanphams = new List<BizSanPham>();
                    int _ID = int.Parse(Request.QueryString["IDSP"]);
                    BizSanPham sanpham = BizSanPham.GetSanPhamByID(_ID);
                    sanphams.Add(sanpham);
                    DataList1.DataSource = sanphams;
                    DataList1.DataBind();
                }
            }
            
        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            bool kt = false;
            int ID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
            BizSanPham sanpham = BizSanPham.GetByID(ID);
            BizDonHang cart = (BizDonHang)Session["cart"];
            int indexItem = cart.FindIndexItem(ID);
            if (indexItem == -1)
            {
                BizChiTietDonHang cartItem = new BizChiTietDonHang()
                {
                   SanPham = sanpham,
                   SoLuong=1
                };
                cart.Items.Add(cartItem);
            }
            else
            {
                cart.Items[indexItem].SoLuong += 1;
            }
            Session["cart"] = cart;
            Response.Redirect("Cart.aspx");

        }
    }
}